package cc.rengu.igas.channel.upac.core.service.trans;

import cc.rengu.igas.channel.upac.common.constant.UpacParamConstant;
import cc.rengu.igas.channel.upac.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upac.common.enums.SignTypeEnum;
import cc.rengu.igas.channel.upac.facade.request.UpacUserSignNoticeRequest;
import cc.rengu.oltp.service.base.BizBaseService;
import cc.rengu.oltp.service.common.dao.SignAcctInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.SignAcctInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.SignAcctInfo;
import cc.rengu.oltp.service.model.RiskMonitorInfo;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.BinInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.annotation.BIZ_PROCESS;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 银联全渠道用户签约通知服务
 * Created by 王成 on 2018/6/3.
 */
public class UpacUserSignNoticeService extends BizBaseService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        return null;
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        UpacUserSignNoticeRequest upacUserSignNoticeRequest = (UpacUserSignNoticeRequest) request;
        /* 获取发卡机构信息 */
        List<BinInfo> binInfoList = (List<BinInfo>) UnifiedCache.get(AppParamConstant.CARD_BIN_CACHE, AppParamConstant.CARD_BIN_CACHE);
        if (null == binInfoList || binInfoList.isEmpty()) {
            rglog.error("获取卡BIN配置信息失败，请更新缓存信息!");
            throw new BizException(RespCodeEnum.GET_CARD_BIN_INFO_ERROR.getRespCode(), RespCodeEnum.GET_CARD_BIN_INFO_ERROR.getRespDesc());
        }
        BinInfo binInfo;
        Optional<BinInfo> binInfoOpt = binInfoList.stream().filter(item -> item.getIssuerCode().equals(upacUserSignNoticeRequest.getPayBankCode())).findFirst();
        if (binInfoOpt.isPresent()) {
            binInfo = binInfoOpt.get();
        } else {
            rglog.error("无此发卡行<{}>配置信息，请更新卡BIN信息表!", upacUserSignNoticeRequest.getPayBankCode());
            throw new BizException(RespCodeEnum.GET_CARD_BIN_INFO_ERROR.getRespCode(), RespCodeEnum.GET_CARD_BIN_INFO_ERROR.getRespDesc());
        }
        /* 登记签约信息表 */
        SignAcctInfo signAcctInfo = new SignAcctInfo();
        signAcctInfo.setInstId(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID));
        signAcctInfo.setSignType(SignTypeEnum.UPAC_PAY.getSignType());
        signAcctInfo.setSignBankNo(upacUserSignNoticeRequest.getPayBankCode());
        signAcctInfo.setSignBankName(binInfo.getIssuerName());
        signAcctInfo.setSignAcctNo(upacUserSignNoticeRequest.getPayAcctNo());
        signAcctInfo.setSignAcctType(upacUserSignNoticeRequest.getPayAcctType());
        signAcctInfo.setSignDate(DateUtil.getCurrentDate());
        signAcctInfo.setSignExpiryDate(UpacParamConstant.DEFAULT_EXPIRY_DATE);
        signAcctInfo.setSignToken(upacUserSignNoticeRequest.getTokenPayInfo().getToken());
        signAcctInfo.setSignTokenInst(signAcctInfo.getSignInst());
        signAcctInfo.setSignStatus(upacUserSignNoticeRequest.getActivateStatus());
        Map<String, String> otherSignData = new HashMap<>();
        if (null != upacUserSignNoticeRequest.getReqReserved()) {
            String[] reqReserved = upacUserSignNoticeRequest.getReqReserved().split(UpacParamConstant.DEFAULT_DELIMITER);
            otherSignData.put("userType", reqReserved[0]);
            otherSignData.put("userId", reqReserved[1]);
            signAcctInfo.setUserId(reqReserved[1]);
        }
        otherSignData.put("trId", upacUserSignNoticeRequest.getTokenPayInfo().getTrId());
        otherSignData.put("token", upacUserSignNoticeRequest.getTokenPayInfo().getToken());
        otherSignData.put("tokenType", upacUserSignNoticeRequest.getTokenPayInfo().getTokenType());
        otherSignData.put("tokenLevel", upacUserSignNoticeRequest.getTokenPayInfo().getTokenLevel());
        otherSignData.put("tokenBegin", upacUserSignNoticeRequest.getTokenPayInfo().getTokenBegin());
        otherSignData.put("tokenEnd", upacUserSignNoticeRequest.getTokenPayInfo().getTokenEnd());
        otherSignData.put("checkFlag", upacUserSignNoticeRequest.getCheckFlag());
        signAcctInfo.setOtherSignData(JSON.toJSONString(otherSignData));
        SignAcctInfoMapper signInfoMapper = new SignAcctInfoMapperImpl();
        int iReturnCode = signInfoMapper.insertSignAcctInfo(signAcctInfo);
        if (0 != iReturnCode) {
            rglog.error("登记签约信息表失败，iReturnCode:<{}>", iReturnCode);
            throw new BizException(RespCodeEnum.INSERT_SIGN_INFO_ERROR.getRespCode(), RespCodeEnum.INSERT_SIGN_INFO_ERROR.getRespDesc());
        }

        /* 查找BIZ_PROCESS的注解，基于注解实现不同场景的业务逻辑 */
        invokeBizAnnoMethod(BIZ_PROCESS.class);

        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.IGAS);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {

    }

    @Override
    protected RiskMonitorInfo getRiskMonitorInfo() {
        return null;
    }
}
